package com.tbit.uqbike.webmanager.controller;

import com.github.pagehelper.PageInfo;
import com.tbit.uqbike.annos.OperateLog;
import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.uqbike.object.pojo.RechargeConfig;
import com.tbit.uqbike.object.pojo.dto.RechargeConfigDTO;
import com.tbit.uqbike.object.pojo.vo.RechargeConfigVO;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.common.entity.view.PageResult;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.service.business.RechargeConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * 充值配置
 *
 * @Author: chen
 * @Description: 充值配置
 * @Date: 创建于 11:22 2020/5/8
 */
@RestController
@RequestMapping("/rechargeConfig")
public class RechargeConfigController extends BaseController {

    @Autowired
    private RechargeConfigService rechargeConfigService;


    /**
     * 新增充值配置
     *
     * @param rechargeConfigDTO 充值对象配置
     * @return 处理结果
     */
    @PostMapping("/add")
    @OperateLog(module = ModuleEnum.RECHARGEGIVE, methods = MethodEnum.RECHARGEGIVE_ADD)
    public Result<String> addRechargeConfig(@RequestBody @Validated RechargeConfigDTO rechargeConfigDTO, HttpServletRequest request) {
        Result<String> checkResult = rechargeConfigDTO.businessLogicCheck();
        if (checkResult.unSuccess()) {
            return checkResult;
        }
        LoginInfo loginInfo = getLogin(request);
        rechargeConfigDTO.setLoginInfo(loginInfo);
        return rechargeConfigService.addRechargeConfig(rechargeConfigDTO);
    }

    /**
     * 新增默认充值配置
     *
     * @return 处理结果
     */
    @PostMapping("/addCustom")
    @OperateLog(module = ModuleEnum.RECHARGEGIVE, methods = MethodEnum.RECHARGEGIVE_ADD)
    public Result<String> addCustomRechargeConfig(@RequestBody RechargeConfig rechargeConfig) {
        Integer accountId = rechargeConfig.getAccountId();
        Integer status = rechargeConfig.getStatus();
        if (accountId == null) {
            return Result.error("请选择运行区域id");
        }
        if (!Objects.equals(status, 0) && !Objects.equals(status, 1)) {
            return Result.error("状态错误，请选择启用或停用");
        }
        return rechargeConfigService.addCustomRechargeConfig(rechargeConfig);
    }

    /**
     * 获取赠送配置分页数据
     *
     * @param accountId 运营区域id
     * @param status    状态 0-未启用  1-启用
     * @param rowCount  分页条数
     * @param pageNO    页码
     * @return 分页数据
     */
    @GetMapping("/listPage")
    public Result<PageResult<RechargeConfigVO>> rechargeConfigListPage(@RequestParam Integer accountId, Integer status,
                                                                       @RequestParam(defaultValue = "10") Integer rowCount,
                                                                       @RequestParam(defaultValue = "1") Integer pageNO) {
        PageInfo<RechargeConfigVO> pageInfo = rechargeConfigService.rechargeConfigListPage(accountId, status, rowCount, pageNO);
        return Result.success(new PageResult<>(pageNO, rowCount, pageInfo.getTotal(), pageInfo.getPages(), pageInfo.getList()));
    }

    /**
     * 获取赠送配置详情
     *
     * @param rechargeConfigId 赠送配置id
     * @return 。
     */
    @GetMapping("/getRechargeConfigById")
    public Result<RechargeConfigVO> selectRechargeConfigById(@RequestParam Integer rechargeConfigId) {
        return rechargeConfigService.selectRechargeConfigById(rechargeConfigId);
    }


    /**
     * 删除赠送配置
     *
     * @param rechargeConfigId 赠送配置id
     * @param request          。
     * @return 。
     */
    @DeleteMapping("/deleteById/{rechargeConfigId}")
    @OperateLog(module = ModuleEnum.RECHARGEGIVE, methods = MethodEnum.RECHARGEGIVE_DEL)
    public Result<String> deleteRechargeConfigById(@PathVariable Integer rechargeConfigId, HttpServletRequest request) {
        // 获取用户信息
        LoginInfo loginInfo = getLogin(request);
        rechargeConfigService.deleteRechargeConfigById(rechargeConfigId, loginInfo);
        return Result.success();
    }


    /**
     * 编辑赠送配置
     *
     * @param rechargeConfigDTO 。
     * @return 。
     */
    @PutMapping("/update")
    @OperateLog(module = ModuleEnum.RECHARGEGIVE, methods = MethodEnum.RECHARGEGIVE_EDIT)
    public Result<String> updateRechargeConfig(@RequestBody RechargeConfigDTO rechargeConfigDTO, HttpServletRequest request) {
        Integer rechargeConfigId = rechargeConfigDTO.getRechargeConfigId();
        if (Objects.isNull(rechargeConfigId)) {
            return Result.error("赠送配置id为空,请选择赠送配置");
        }
        Result<String> checkResult = rechargeConfigDTO.businessLogicCheck();
        if (checkResult.unSuccess()) {
            return checkResult;
        }
        LoginInfo loginInfo = getLogin(request);
        rechargeConfigDTO.setLoginInfo(loginInfo);
        return rechargeConfigService.updateRechargeConfig(rechargeConfigDTO);
    }

    /**
     * 禁用赠送配置
     *
     * @param rechargeConfig 赠送配置id
     * @return 。
     */
    @PutMapping("/disable")
    @OperateLog(module = ModuleEnum.RECHARGEGIVE, methods = MethodEnum.RECHARGEGIVE_SWITCH)
    public Result<String> disable(@RequestBody RechargeConfig rechargeConfig) {
        if (Objects.isNull(rechargeConfig.getRechargeConfigId())) {
            return Result.error("赠送配置id为空,请选择赠送配置");
        }
        return rechargeConfigService.disableRechargeConfig(rechargeConfig.getRechargeConfigId());
    }

    /**
     * 启用赠送配置
     *
     * @param rechargeConfig 赠送配置id
     * @return 。
     */
    @PutMapping("/enable")
    @OperateLog(module = ModuleEnum.RECHARGEGIVE, methods = MethodEnum.RECHARGEGIVE_SWITCH)
    public Result<String> enable(@RequestBody RechargeConfig rechargeConfig) {
        if (Objects.isNull(rechargeConfig.getRechargeConfigId())) {
            return Result.error("赠送配置id为空,请选择赠送配置");
        }
        return rechargeConfigService.enable(rechargeConfig.getRechargeConfigId());
    }


}
